package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/trans/common"
	servicecommon "jlpay-sdk-go/pkg/jlpay/services/trans/common"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 支付类型常量
const (
	PAY_TYPE_WECHAT = "wxpay"    // 微信支付
	PAY_TYPE_ALIPAY = "alipay"   // 支付宝支付
	PAY_TYPE_UNION  = "unionpay" // 银联支付
)

// 终端类型常量
const (
	TERMINAL_TYPE = "N50007A8890" // 终端号
)

// 构建预授权完成请求
func buildPreAuthCompleteRequest() *common.PreAuthCompleteRequest {
	request := common.NewPreAuthCompleteRequest()

	// 设置请求参数
	request.SetMchID("849584358120018")
	request.SetTermNo("60131234")
	request.SetPayType(PAY_TYPE_WECHAT)
	request.SetOutTradeNo("ld201911023465529")
	request.SetOriOutTradeNo("ld2019102216465525")
	request.SetOriTransactionID("80011557201909171853011568")
	request.SetGuaranteeAuthCode("TEST_AUTH_CODE")
	request.SetTotalFee("100")
	request.SetRemark("测试预授权完成")
	request.SetDeviceInfo(TERMINAL_TYPE)
	request.SetMchCreateIP("127.0.0.1")
	request.SetTransAddress("北京市朝阳区")
	request.SetAreaCode("110000")
	request.SetLongitude("116.397128")
	request.SetLatitude("39.916527")

	return request
}

// 预授权完成示例
func preAuthCompleteDemo(preAuthCompleteService *servicecommon.PreAuthCompleteService) {
	// 创建预授权完成请求
	request := buildPreAuthCompleteRequest()

	// 序列化请求数据并打印
	requestMap, _ := request.ToMap()
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("预授权完成请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := preAuthCompleteService.PreAuthComplete(request)
	if err != nil {
		utils.Errorf("预授权完成请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("预授权完成")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 预授权完成示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和预授权完成服务
	config := demo.CreateDemoConfig()
	preAuthCompleteService := servicecommon.NewPreAuthCompleteService(config)

	preAuthCompleteDemo(preAuthCompleteService)

	utils.Info("=============================")
	utils.Info("===== 预授权完成示例执行完毕 =====")
	utils.Info("=============================")
}
